<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div>
            <div class="handle-box">
                <el-dialog :title="title" :visible.sync="dialogFormVisible">
                    <el-form :model="addForm">
                        <el-form-item label="用户名" :label-width="formLabelWidth">
                            <el-input v-model="addForm.user_name" placeholder="20字以内" auto-complete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="角色" :label-width="formLabelWidth">
                            <el-select v-model="addForm.role_id" placeholder="请选择">
                                <el-option v-for="role in roles" :key="role.id" :label="role.name" :value="role.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="密码" :label-width="formLabelWidth">
                            <el-input type="password" v-model="addForm.password" placeholder="20字以内" auto-complete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" :label-width="formLabelWidth">
                            <el-input type="password" v-model="addForm.confirm_password" placeholder="20字以内" auto-complete="off"></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button type="primary" @click="submit">确 定</el-button>
                    </div>
                </el-dialog>
                <el-button type="success" @click="add">添加账号</el-button>
            </div>
            <div>
                <el-table :data="accounts" border fit>
                    <el-table-column prop="user_name" label="账号名" align="center">
                    </el-table-column>
                    <el-table-column prop="role_name" label="角色" align="center">
                    </el-table-column>
                    <el-table-column prop="last_login" label="最后登陆时间" :formatter="time" align="center">
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button @click="put(scope.row)">编辑</el-button>
                            <el-button @click="del(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="block">
                <el-pagination @current-change="handleCurrentChange" :current-page="num" layout="total,  prev, pager, next, jumper" :total="totalElements">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
import { addAccount, allAccount, putAccount, delAccount, allRoles } from '../../api/server.js'
import { time_format } from '../../utils/date.js'
import md5 from 'js-md5'
import { setStore, getStore, ras_public } from '../../utils/local.js'

export default {
    data() {
        return {
            totalElements: 10,
            num: 1,
            accounts: [],
            dialogFormVisible: false,
            addForm: {},
            title: '',
            formLabelWidth: '120px',
            roles: []
        }
    },
    created() {
        this.get_account(1, 10)
        this.get_role(1, 20)
    },
    computed: {
        check() {
            if (this.addForm.password && this.addForm.confirm_password && this.addForm.password !== this.addForm.confirm_password) {
                this.$message.error('二次密码不一致')
                return false
            }
            return true
        }
    },
    methods: {
        addArticle() {
            this.$router.push({
                path: '/articleEdit'
            })
        },
        handleCurrentChange(val) {
            this.num = val
            this.get_account(val, 10)
        },
        get_account(page, pageSize) {
            allAccount({ page, pageSize })
                .then(res => {
                    this.accounts = res.result
                    this.totalElements = res.resultNum
                })
        },
        add() {
            this.addForm = {}
            this.title = '添加用户'
            this.dialogFormVisible = true
        },
        get_role(page, pageSize) {
            allRoles({ page, pageSize })
                .then(res => {
                    this.roles = res.result
                })
        },
        time(row) {
            if (row.last_login)
                return time_format(row.last_login, 'yyyy-MM-dd hh:mm:ss')
        },
        put(row) {
            this.addForm = row
            this.dialogFormVisible = true
        },
        del(id) {
            delAccount({ id })
                .then(res => {
                    this.$message.success('删除成功')
                    this.get_account(1, 10)
                })
        },
        submit() {
            if (this.check) {
                // console.log('submit')
                let account = this.addForm
                account.roleId = account.role_id
                let jse = new this.$jsEncrypt.JSEncrypt()
                jse.setPublicKey(ras_public)
                let pwd = account.password
                let password = jse.encrypt(md5(pwd).toLowerCase())
                account.passwd = password
                account.name = account.user_name
                console.log('account', account)
                if (account.id) {
                    putAccount(account)
                        .then(res => {
                            this.$message.success('修改成功')
                            this.get_account(1, 10)
                            this.dialogFormVisible = false
                        })
                } else {
                    addAccount(account)
                        .then(res => {
                            this.$message.success('添加成功')
                            this.get_account(1, 10)
                            this.dialogFormVisible = false
                        })
                }

            }
        }
    }
}

</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>